// 单独封装文件上传，使用uni.uploadFile处理
export function uploadFile(filePath) {
  return new Promise((resolve, reject) => {
    const config = require('@/config.js');
    const baseUrl = config.baseUrl;
    const token = require('@/utils/auth.js').getToken();

    uni.uploadFile({
      url: baseUrl + '/file/upload', // 拼接baseUrl和上传接口路径
      filePath,
      name: 'file',
      header: {
        'Authorization': `Bearer ${token}` // 携带Token鉴权
      },
      success: (res) => {
        try {
          const data = JSON.parse(res.data);
          if (data.code === 200) {
            resolve(data);
          } else {
            reject(new Error(data.msg || '图片上传失败'));
          }
        } catch (e) {
          reject(new Error('解析响应失败'));
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}